#!/usr/bin/python
# -*- coding: utf-8 -*-

#This file is part of pyLot library.
#
# pyLot is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyLot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with pyLot.  If not, see <http://www.gnu.org/licenses/>.

__author__ = u'Pierre Puiseux, Guillaume Baty'
__copyright__ = u"Copyright 2011-2012 (C) andheo, Université de Pau et des Pays de l'Adour"
__credits__ = [u'Pierre Puiseux', u'Guillaume Baty']
__date__ = "15 juin 2010"
__license__ = "GNU Lesser General Public License"

import sys
import optparse

from pyLot.core import (unicode2_to_ASCIIstr, uprint)

class pyLotOptionParser (optparse.OptionParser):
  """
  Based on OptionParser, add "append" parameter to add_option.
  If append=a_list, add option names to this list
  """
  def __init__ (self, *t, **d):
    self._auto_options = {}
    self.console = ['console']
    optparse.OptionParser.__init__(self, *t, **d)

#  def _get_encoding(self, file):
#    return ENCODING

  def add_option(self, *args, **kwargs):
    if 'userlist' in kwargs :
      userlist = kwargs.pop('userlist')
    else :
      userlist = None
    option = optparse.OptionParser.add_option(self, *args, **kwargs)

    if userlist :
      self.userlist.append(option.dest)

    return option

  def in_userlist (self, options, userlist) :
    for opt, value in options.__dict__.items() :
      if value and opt in userlist :
        return True
    return False

  def createOptionsFromBook (self, book, alias=None):

    if alias is None :
      if book.alias:
        alias = book.alias
      else:
        alias = book.name
      alias = unicode2_to_ASCIIstr(alias)
    group_opt = pyLotOptionGroup(self, alias)

    def generic_opt (option, opt, value, parser) :
      book[option.dest].value = value

    self._auto_options[alias] = generic_opt

    for optid, datum in book.items() :
      optid = unicode2_to_ASCIIstr(optid)
      group_opt.add_option("--set-%s" % optid,
        action="callback", dest=optid, nargs=1, type='str', callback=generic_opt,
        help=unicode2_to_ASCIIstr(datum.tooltip),
        metavar='%s [%s]' % (datum.nature.__name__, unicode2_to_ASCIIstr(datum.value)))
    return group_opt

class pyLotOptionGroup(optparse.OptionGroup) :

  def add_option(self, *args, **kwargs):
    if 'userlist' in kwargs :
      userlist = kwargs.pop('userlist')
    else :
      userlist = None
    try :
      option = optparse.OptionGroup.add_option(self, *args, **kwargs)
    except optparse.OptionConflictError :
      uprint(tr_(u'option %s yet exists') % args, file=sys.stderr)
    else :
      if userlist is not None :
        userlist.append(option.dest)

      return option
